Completed
Push — master ( 99bbd9...6ab302 )
by Askupa
01:25
created

Amarkal.settings._updateValues   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
nc 3
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A Amarkal.settings._postData 0 3 1
1
/**
2
 * Asynchronously save all settings in the current settings page to the database.
3
 * Shows an error notification if errors occur. Shows a success notification
4
 * otherwise.
5
 * 
6
 * @param {Function} done
7
 */
8
Amarkal.settings.save = function( done ) 
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
{
10
    Amarkal.settings._postData('save',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
11
        
12
        Amarkal.settings._clearErrors();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
13
        
14
        if(!$.isEmptyObject(res.errors)) {
15
            for(var name in res.errors) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
16
                var $comp = $('[amarkal-component-name="'+name+'"]');
17
                
18
                $comp.amarkalUIComponent('makeInvalid');
19
                $comp.parent()
20
                     .children('.amarkal-settings-error')
21
                     .addClass('amarkal-visible')
22
                     .html(res.errors[name]);
23
            }
24
            Amarkal.settings.notifier.error('Some errors have occured, see below for more information.');
25
        }
26
        else {
27
            Amarkal.settings.notifier.success('Settings saved', 2000);
28
        }
29
30
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
31
        
32
        done();
33
    });
34
};
35
36
/**
37
 * Asynchronously reset all settings in the current settings page to their 
38
 * default values and erase all data from the database. Shows a success 
39
 * notification upon completion.
40
 * 
41
 * @param {Function} done
42
 */
43
Amarkal.settings.reset = function( done ) 
44
{
45
    Amarkal.settings._postData('reset',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
        
47
        Amarkal.settings._clearErrors();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
48
        Amarkal.settings.notifier.success('Default settings applied', 2000);
49
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
50
        
51
        done();
52
    });
53
};
54
55
Amarkal.settings._clearErrors = function()
56
{
57
    // Reset all components
58
    $('.amarkal-ui-component').amarkalUIComponent('reset');
59
    $('.amarkal-settings-error').removeClass('amarkal-visible').html('');
60
};
61
62
/**
63
 * Update all components in the current settings page with the given values.
64
 * 
65
 * @param {Object} values
66
 * @param {Array} errors
67
 */
68
69
/**
70
 * Send serialized form data to be processed in the backend by the function given
71
 * in the 'action' variable.
72
 * 
73
 * @param {string} action
74
 * @param {Function} done
75
 */
76
Amarkal.settings._postData = function( action, done )
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
77
{
78
    var data = $('#amarkal-settings-form').amarkalUIForm('getData');
79
    $('#amarkal-settings-form').find('input[name^="_amarkal"]').each(function(){
80
        data[$(this).attr('name')] = $(this).val();
81
    });
82
83
    $.post(ajaxurl, {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
84
        action: 'amarkal_settings_'+action,
85
        data: data
86
    }, done);
87
};